package org.example.builder;

import org.example.bean.Constants;
import org.example.bean.FiledInfo;
import org.example.bean.TableInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class BuildBase {
    private static Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static void execute(){
        ArrayList<String> headerInfoList = new ArrayList();

        //生成枚举enums
        headerInfoList.add("package " +Constants.PACKAGE_ENUMS );
        build(headerInfoList,"DateTimePatternEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();
        //生成工具类utils
        headerInfoList.add("package " +Constants.PACKAGE_UTILS );
        build(headerInfoList,"DateUtil", Constants.PATH_UTILS);
        headerInfoList.clear();

        //生成baseMapper
        headerInfoList.add("package " +Constants.PACKAGE_MAPPER );
        build(headerInfoList,"BaseMapper", Constants.PATH_MAPPER);
        headerInfoList.clear();

        //生成PageSize
        headerInfoList.add("package " +Constants.PACKAGE_ENUMS );
        build(headerInfoList,"PageSize", Constants.PATH_ENUMS);
        headerInfoList.clear();
        //ResponseCodeEnum
        headerInfoList.add("package " +Constants.PACKAGE_ENUMS );
        build(headerInfoList,"ResponseCodeEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();

        //生成SimplePage
        headerInfoList.add("package " +Constants.PACKAGE_QUERY);
        headerInfoList.add("import " +Constants.PACKAGE_ENUMS + ".*");
        build(headerInfoList,"SimplePage", Constants.PATH_QUERY);
        headerInfoList.clear();
        //生成BaseParam
        headerInfoList.add("package " +Constants.PACKAGE_QUERY);
        build(headerInfoList,"BaseQuery", Constants.PATH_QUERY);
        headerInfoList.clear();
        //生成PaginationResult
        headerInfoList.add("package " +Constants.PACKAGE_VO);
        build(headerInfoList,"PaginationResultVO", Constants.PATH_VO);
        headerInfoList.clear();
        //生成PaginationResult
        headerInfoList.add("package " +Constants.PACKAGE_VO);
        build(headerInfoList,"ResponseVO", Constants.PATH_VO);
        headerInfoList.clear();
        //生成PaginationResult
        headerInfoList.add("package " +Constants.PACKAGE_EXCEPTION);
        headerInfoList.add("import com.example.enums.ResponseCodeEnum;");
        build(headerInfoList,"BusinessException", Constants.PATH_EXCEPTION);
        headerInfoList.clear();
        //ABaseController
        headerInfoList.add("package " +Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import com.example.entity.vo.ResponseVO;");
        headerInfoList.add("import com.example.enums.ResponseCodeEnum;");
        build(headerInfoList,"ABaseController", Constants.PATH_CONTROLLER);
        headerInfoList.clear();
        //ABaseController
        headerInfoList.add("package " +Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import com.example.entity.vo.ResponseVO;");
        headerInfoList.add("import com.example.enums.ResponseCodeEnum;");
        headerInfoList.add("import com.example.exception.BusinessException;");
        build(headerInfoList,"AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);
        headerInfoList.clear();

    }
    public static void build(List<String> headerInfoList, String fileName,String outPutPath)  {

        File folder = new File(outPutPath);
        if(!folder.exists()) {
            folder.mkdirs();
        }
        File javaFile = new File(outPutPath, fileName + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inr = null;
        BufferedReader bf = null;
        try {
            out = new FileOutputStream(javaFile);
            outw = new OutputStreamWriter(out,"utf-8");
            bw = new BufferedWriter(outw);

            String templatePath = BuildBase.class.getClassLoader()
                    .getResource("template/" + fileName + ".txt").getPath();
            in = new FileInputStream(templatePath);
            inr = new InputStreamReader(in,"utf-8");
            bf = new BufferedReader(inr);

            //导入包
            for (String s:headerInfoList) {
                bw.write(s + ";");
                bw.newLine();
                if (s.contains("package")) {
                    bw.newLine();
                }
            }
            String lineInfo = null;
            while ((lineInfo = bf.readLine()) != null) {
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();

        }catch (Exception e) {
            logger.error("生成基础类：{}，失败",fileName,e);
        } finally {
            if (bf != null) {
                try {
                    bf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (inr != null) {
                try {
                    inr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }
}
